﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;
using System.Linq;

namespace Com
{
    /// <summary>
    /// Interaction logic for ucColdStorageRoom.xaml
    /// </summary>
    public partial class ucColdStorageRoom : UserControl
    {
        tb_storage_room_type room;
        int volumeIndex = -1;
        public ucColdStorageRoom()
        {
            this.InitializeComponent();
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            loadData();
            enableButtons();
        }
        public void loadData()
        {
            cbxDescription.ItemsSource = LibraryManagerInCom.RoomTypeList;
            cbxDescription.DisplayMemberPath = "storage_room";
            cbxDescription.SelectedIndex = 0;
        }

        public void reload()
        {
            cbxDescription.ItemsSource = null;
            cbxDescription.Items.Clear();
            foreach (var item in LibraryManagerInCom.RoomTypeList)
            {
                cbxDescription.Items.Add(item);
            }
            cbxDescription.DisplayMemberPath = "storage_room";
            cbxDescription.SelectedIndex = 0;
        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            room = (tb_storage_room_type)cbxDescription.SelectedItem;
            loadCoefficient();
        }

        private void txtSpecifictHeat_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxRoomVolume.SelectedIndex == 0)
            {
                volumeIndex = 0;
            }
            if (cbxRoomVolume.SelectedIndex == 1)
            {
                volumeIndex = 1;
            }
            if (cbxRoomVolume.SelectedIndex == 2)
            {
                volumeIndex = 2;
            }
            loadCoefficient();

        }
        public void loadCoefficient()
        {
            int coefficient = -1;
            if (room != null && volumeIndex >= 0)
            {
                if (volumeIndex == 0)
                {
                    coefficient = Convert.ToInt32(room.C50m2);
                }
                if (volumeIndex == 1)
                {
                    coefficient = Convert.ToInt32(room.C50_150m2);
                }
                if (volumeIndex == 2)
                {
                    coefficient = Convert.ToInt32(room.C_150m2);
                }

                if (coefficient != 0)
                {
                    txtCoefficient.Text = coefficient.ToString();
                }
                else
                {
                    txtCoefficient.Text = "No data available";
                }

            }

        }


        private void enableButtons()
        {
            btnSave.IsEnabled = false;
            btnCancel.IsEnabled = false;

            btnNew.IsEnabled = true;
            btnCopy.IsEnabled = true;
            btnDelete.IsEnabled = true;
        }
        private void disableButtons()
        {
            btnSave.IsEnabled = true;
            btnCancel.IsEnabled = true;

            btnNew.IsEnabled = false;
            btnCopy.IsEnabled = false;
            btnDelete.IsEnabled = false;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            tb_storage_room_type room = new tb_storage_room_type();
            room.storage_room = cbxDescription.Text;
            if (cbxRoomVolume.SelectedIndex == 0)
            {
                room.C50m2 = Convert.ToInt16(txtCoefficient.Text);
                LibraryManagerInCom.addRoomType(room, 0);
            }
            if (cbxRoomVolume.SelectedIndex == 1)
            {
                room.C50_150m2 = Convert.ToInt16(txtCoefficient.Text);
                LibraryManagerInCom.addRoomType(room, 1);
            }
            if (cbxRoomVolume.SelectedIndex == 2)
            {
                room.C_150m2 = Convert.ToInt16(txtCoefficient.Text);
                LibraryManagerInCom.addRoomType(room, 2);
            }
            enableButtons();
            reload();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            enableButtons();
            loadData();
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            cbxDescription.Text = "";
            cbxRoomVolume.SelectedIndex = 0;
            txtCoefficient.Text = "";
            disableButtons();
        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            tb_storage_room_type room = (tb_storage_room_type)cbxDescription.SelectedItem;
            tb_storage_room_type room2 = LibraryManagerInCom.copyRoomType(room);
            cbxDescription.ItemsSource = null;
            cbxDescription.Items.Clear();
            cbxDescription.Items.Add(room2);
            cbxDescription.SelectedItem = room2;
            disableButtons();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                tb_storage_room_type room = (tb_storage_room_type)cbxDescription.SelectedItem;
                LibraryManagerInCom.delRoomType(room);
                reload();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
    }
}